import pyglet
from pyglet.gl import *

import cocos
from cocos.director import director

from gamelib import music

class Success(cocos.scene.Scene):
    def __init__(self, level):
        super(Success, self).__init__()
        self.coin_reward = level.ground.get('coin-reward')
        self.xp_reward = level.ground.get('xp-reward')
        self.unlocks = level.ground.get('unlocks', '')
        self.party = level.party
        for character in self.party.characters:
            if not character.is_active():
                [self.party.items.append(character.body.get(s)) for s in
                 character.body.slots if s is not None]
                [self.party.items.append(i) for i in character.pack]
                self.party.characters.remove(character)
        self.npcs = level.npcs

    def add_label(self, text, x, y, font_size):
        l = cocos.text.Label(text, (x, y), color=(0, 0, 0, 255),
            anchor_y='center', anchor_x='center', font_size=font_size)
        self.add(l)
        return y - l.element.content_height

    def on_enter(self):
        super(Success, self).on_enter()

        self.add(GradientLayer((255, 255, 200, 255), (255, 255, 255, 255)))

        w, h = director.get_window_size()
        y = self.add_label('Success!', w//2, 3*h//4, 32)

        y = self.add_label('You have been awarded:', w//2, y-32, 24)

        # XXX animate me
        y = self.add_label('%d coins for the party, and'%self.coin_reward, w//2, y, 24)
        y = self.add_label('%d experience points each!'%self.xp_reward, w//2, y, 24)

        for map in self.unlocks.split(','):
            y = self.add_label('New location(s) unlocked', w//2, y, 24)
            self.party.maps_seen.append(map.strip())

        self.party.coins += self.coin_reward
        self.party.level += 1
        for character in self.party.characters:
            character.experience += self.xp_reward

        y = self.add_label('(press any key)', w//2, y-64, 24)

        self.music_player = music.play('music_victory_t.ogg')

        director.window.push_handlers(self)

    def on_key_press(self, key, modifier):
        self.end()
        return True

    def on_exit(self):
        super(Success, self).on_exit()
        director.window.pop_handlers()
        # XXX fade volume down and fade menu out
        self.music_player.pause()
        self.music_player = None


class GradientLayer(cocos.layer.Layer):
    def __init__(self, bottom, top):
        assert isinstance(bottom, tuple)
        assert isinstance(top, tuple)
        self.bottom = bottom
        self.top = top
        super(GradientLayer, self).__init__()

    def on_enter(self):
        super(GradientLayer, self).on_enter()
        self.batch = pyglet.graphics.Batch()

        w, h = director.get_window_size()
        self.l = self.batch.add(4, GL_QUADS, None,
            ('v2i', (0, 0, w, 0, w, h, 0, h)),
            ('c4B', self.bottom * 2 + self.top * 2)
        )

    def on_exit(self):
        super(GradientLayer, self).on_exit()
        self.l.delete()
        self.batch = None

    def draw(self, x=0, y=0):
        self.transform()
        self.batch.draw()

